package cn.distant.tank;

import java.awt.*;

/**
 * @author distantstar
 * @version 1.0
 * @date 2021
 * @description cn.distant.tank
 * @since JDK1.8
 */
public class Bullet {

    private static final int SPEED = Integer.parseInt((String)PropertiesMgr.getKey("bulletSpeed"));
    private int x = 0, y = 0;
    private Dir dir;
    static int width = ResourceMgr.bulletD.getWidth(), height = ResourceMgr.bulletD.getHeight();
    private boolean live = true;
    private TankFrame tf;
    private Group group;
    private Rectangle rect = new Rectangle();

    public Bullet(int x, int y, Dir dir, TankFrame tf, Group group) {
        this.x = x;
        this.y = y;
        this.dir = dir;
        this.tf = tf;
        this.group = group;

        this.rect.x = this.x;
        this.rect.y = this.y;
        this.rect.width = Bullet.width;
        this.rect.height = Bullet.height;
    }

    public void paint(Graphics g) {
        // 越界删除子弹
        if (!live) {
            tf.bullets.remove(this);
        }

        // 根据方向设置不同的子弹图片
        switch (dir) {
            case RIGHT:
                width = ResourceMgr.bulletR.getWidth();
                height = ResourceMgr.bulletR.getHeight();
                g.drawImage(ResourceMgr.bulletR, x, y, null);
                break;
            case LEFT:
                width = ResourceMgr.bulletL.getWidth();
                height = ResourceMgr.bulletL.getHeight();
                g.drawImage(ResourceMgr.bulletL, x, y, null);
                break;
            case UP:
                width = ResourceMgr.bulletU.getWidth();
                height = ResourceMgr.bulletU.getHeight();
                g.drawImage(ResourceMgr.bulletU, x, y, null);
                break;
            case DOWN:
                width = ResourceMgr.bulletD.getWidth();
                height = ResourceMgr.bulletD.getHeight();
                g.drawImage(ResourceMgr.bulletD, x, y, null);
                break;
            default:
                break;
        }

        // 调用移动函数
        move();
    }

    /**
     * 移动
     */
    private void move() {
        switch (dir) {
            case RIGHT:
                x += SPEED;
                break;
            case LEFT:
                x -= SPEED;
                break;
            case UP:
                y -= SPEED;
                break;
            case DOWN:
                y += SPEED;
                break;
            default:
                break;
        }

        this.rect.x = this.x;
        this.rect.y = this.y;

        if (x < 0 || y < 0 || x > TankFrame.GAME_WIDTH || y > TankFrame.GAME_HEIGHT) {
            live = false;
        }
    }

    /**
     * 检测子弹与敌军坦克是否相碰撞
     * @param tank 敌军坦克
     */
    public void collisionDetection(Tank tank) {
        // 判断子弹的来向是友军还是敌军
        if (this.group == tank.getGroup()) {
            return;
        }

        // 判断两个矩形是否相交
        if (rect.intersects(tank.getRect())) {
            this.die();
            tank.die();

            int ex = tank.getX() + ((tank.getWidth() - Explode.width) >> 1);
            int ey = tank.getY() + ((tank.getHeight() - Explode.height) >> 1);

            tf.explodes.add(new Explode(ex, ey, this.tf));
        }
    }

    private void die() {
        this.live = false;
    }
}
